/**
 *******************************************************************************
 * @FileName  : tlsr825x_systimer.h
 * @Author    : GaoQiu
 * @CreateDate: 2020-09-10
 * @Copyright : Copyright(C) GaoQiu
 *              All Rights Reserved.
 *
 *******************************************************************************
 *
 * The information contained herein is confidential and proprietary property of
 * GaoQiu and is available under the terms of Commercial License Agreement
 * between GaoQiu and the licensee in separate contract or the terms described
 * here-in.
 *
 * This heading MUST NOT be removed from this file.
 *
 * Licensees are granted free, non-transferable use of the information in this
 * file under Mutual Non-Disclosure Agreement. NO WARRENTY of ANY KIND is provided.
 *
 *******************************************************************************
 */
#ifndef _TLSR825X_SYSTMER_H
#define _TLSR825X_SYSTMER_H

#include "libc/types.h"
#include "tlsr825x.h"

/*!  System timer tick per usecond */
#define SYS_TIM_TICK_PER_US    16

#define SYS_TIM_US_TO_TICK(us)  ((us) * SYS_TIM_TICK_PER_US)
#define SYS_TIM_MS_TO_TICK(ms)  ((ms) * 1000 * SYS_TIM_TICK_PER_US)

#define SYS_TIM_TICK_TO_US(t)    ((t) / SYS_TIM_TICK_PER_US)

/*!  System timer update interrupt. */
#define SYSTIM_IT_UPD          SYS_TIM_IRQ_EN

/**
 * @brief : Enable system timer.
 * @param : none.
 * @return: none.
 */
static inline void SysTimer_Enable(void)
{
	SYS_TIM->CTRL_STATE |= SYS_TIM_EN;
}

/**
 * @brief : Disable system timer.
 * @param : none.
 * @return: none.
 */
static inline void SysTimer_Disable(void)
{
	SYS_TIM->CTRL_STATE &= ~SYS_TIM_EN;
}

/**
 * @brief : Set System timer capture value.
 * @param : none.
 * @return: none.
 */
static inline void SysTimer_SetCapture(uint32_t cap)
{
	SYS_TIM->CAP = (uint32_t)(cap);
}

/**
 * @brief : Get System timer capture value.
 * @param : none.
 * @return: none.
 */
static inline uint32_t SysTimer_GetCapture(void)
{
	return SYS_TIM->CAP;
}

/**
 * @brief : Get System timer current tick.
 * @param : none
 * @return: return current system timer tick value.
 */
static inline uint32_t SysTimer_GetCurTick(void)
{
	return SYS_TIM->CNT;
	//return REG_32(0x740);
}

/**
 * @brief : Set 32K tracking value.
 * @param : nowTick      current tick value.
 * @param : timerUs      time
 * @return: true or false.
 */
static inline bool_t SysTimer_IsTimeExpired(uint32_t refTick, uint32_t timerUs)
{
	return ((uint32_t)(SYS_TIM->CNT - (uint32_t)(refTick)) > (uint32_t)((timerUs) * SYS_TIM_TICK_PER_US));
}

/**
 * @brief : Set 32K tracking value.
 * @param : none.
 * @return: none.
 */
static inline void Set32kTrackInitValue(uint16_t x)
{
	SYS_TIM->TRACKING = (uint16_t)(x);
}

/**
 * @brief : Get 32K tracking value.
 * @param : none.
 * @return: none.
 */
static inline uint16_t Get32kTrackValue(void)
{
	return SYS_TIM->TRACKING;
}

/**
 * @brief : Set system timer specified interrupt.
 * @param : SYSTIM_IT     only can be SYSTIM_IT_UPD.
 * @return: none.
 */
static inline void SysTimer_ITConfig(uint8_t SYSTIM_IT, bool_t enable)
{
	if(enable){
		SYS_TIM->CTRL |= SYSTIM_IT;
	}
	else{
		SYS_TIM->CTRL &= ~SYSTIM_IT;
	}
}

/**
 * @brief : Initialize system timer.
 *          Note: set counter to 0, set 32k tracking to 8000 and enable tracking.
 * @return: none.
 */
void SysTimer_Init(void);

/**
 * @brief : Set system timer counter Initialization value.
 * @param : value    counter value.
 * @return: none.
 */
void SysTimer_SetInitCounter(uint32_t value);

/**
 * @brief : delay us.
 *          !!! Note: only this API is usable after system timer is enabled.
 * @param : us     range of 1 ~ UINT32_MAX/SYS_TIM_TICK_PER_US
 * @return: none.
 */
void delay_us(uint32_t us);

/**
 * @brief : delay ms.
 *          !!! Note: only this API is usable after system timer is enabled.
 * @param : ms     range of 1 ~ UINT32_MAX/SYS_TIM_TICK_PER_US/1000
 * @return: none.
 */
void delay_ms(uint32_t ms);

#endif
